package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.ViewPage;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.OssUploadService;
import com.woniu.adopt.service.ViewPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.UUID;

@Api(value = "本接口是操作轮播图管理功能",description = "本接口是操作轮播图管理功能")
@RestController
@RequestMapping("ViewPage")
public class ViewPageController {
    @Value("${imgPath}")
    private String imgPath;

    @Autowired
    OssUploadService uploadService;
    
    @Autowired
    private ViewPageService viewPageService;

    @ApiOperation(value = "轮播图列表分页")
    @GetMapping("/list")
    public ResultResponse<PageInfo<ViewPage>> list(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                    @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                    String title){
        ResultResponse<PageInfo<ViewPage>> result = null;
        try{
            PageInfo<ViewPage> petPageInfo = viewPageService.queryByPage(now, size,title);
            result = new ResultResponse<>(200,"查询轮播图成功!",petPageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询轮播图失败!");
        }
        return result;
    }

    @ApiOperation(value = "根据id查询轮播图")
    @GetMapping("/findPetByKey")
    public ResultResponse<ViewPage> queryPetByKey(@RequestParam(value = "id") Integer id){
        ResultResponse<ViewPage> result = null;
        try {
            ViewPage pet = viewPageService.queryPetByKey(id);
            result = new ResultResponse<>(200,"查询轮播图成功!",pet);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询轮播图失败!");
        }
        return result;
    }

    @ApiOperation(value = "修改轮播图")
    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody ViewPage pet){
        ResultResponse<Void> result = null;
        try {
            pet.setUpdateTime(new Date());
            viewPageService.modify(pet);
            result = new ResultResponse<>(200,"修改轮播图成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改轮播图失败!");
        }
        return result ;
    }

    @ApiOperation(value = "新增轮播图")
    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody ViewPage pet){
        ResultResponse<Void> result = null;
        try {
            pet.setInsertTime(new Date());
            viewPageService.add(pet);
            result = new ResultResponse<>(200,"添加轮播图成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加轮播图失败!");
        }
        return result ;
    }

    @ApiOperation(value = "删除轮播图")
    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;
        try {
            viewPageService.removeByKey(id);
            result = new ResultResponse<>(200,"删除轮播图成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除轮播图失败!");
        }
        return result ;
    }

    @PostMapping("/uploadImg")
    public ResultResponse<String> uploadImg(@RequestParam(value = "uploadImg") MultipartFile multipartFile) {
        ResultResponse<String> result = null;
        try{
            //获取上传的文件名字
            String filename=multipartFile.getOriginalFilename();
            filename = UUID.randomUUID().toString().replaceAll("-","")+filename.substring(filename.lastIndexOf("."));
            File f = new File(imgPath);
            if(!f.exists()){
                f.mkdirs();
            }
            File file = new File(f,filename);
            multipartFile.transferTo(file);
            result = new ResultResponse<>(200,"上传成功",filename);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(200,"上传失败");
        }
        return result;
    }

    @PostMapping("/upload")
    public ResultResponse<String> upload(@RequestParam("file") MultipartFile multipartFile){
        ResultResponse<String> resultResponse = null;
        String result = uploadService.uploadImg(multipartFile);
        System.out.println(result);
        resultResponse = new ResultResponse<>(200,"上传成功",result);
        return resultResponse;
    }
}
